package com.jie.langlangmall.portal.controller;

import com.jie.langlangmall.common.api.CommonResult;
import com.jie.langlangmall.model.OmsCartItem;
import com.jie.langlangmall.portal.domain.CartProduct;
import com.jie.langlangmall.portal.domain.CartPromotionItem;
import com.jie.langlangmall.portal.service.OmsCartItemService;
import com.jie.langlangmall.portal.service.UmsMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Jie
 * @Date 2024/11/27
 * @Email 3217998214@qq.com
 * @Description 购物车系统
 */
@RestController
@Tag(name = "OmsCartItemController", description = "购物车管理")
@RequestMapping("/cart")
public class OmsCartItemController {

    @Autowired
    private OmsCartItemService cartItemService;
    @Autowired
    private UmsMemberService memberService;

    @Operation(summary = "添加商品到购物车")
    @PostMapping("/add")
    public CommonResult<?> add(@RequestBody OmsCartItem cartItem) {
        cartItemService.add(cartItem);
        return CommonResult.success();
    }

    @Operation(summary = "获取当前用户的购物车列表")
    @GetMapping("/list")
    public CommonResult<List<OmsCartItem>> list() {
        List<OmsCartItem> cartItemList = cartItemService.list(memberService.getCurrentMember().getId());
        return CommonResult.success(cartItemList);
    }

    @Operation(summary = "获取当前用户的购物车列表,包括促销信息")
    @GetMapping("/list/promotion")
    public CommonResult<List<CartPromotionItem>> listPromotion(@RequestParam(required = false) List<Long> cartIds) {
        List<CartPromotionItem> cartPromotionItemList = cartItemService.listPromotion(memberService.getCurrentMember().getId(), cartIds);
        return CommonResult.success(cartPromotionItemList);
    }

    @Operation(summary = "修改购物车中指定商品的数量")
    @GetMapping("/update/quantity")
    public CommonResult<?> updateQuantity(@RequestParam Long id,
                                          @RequestParam Integer quantity) {
        cartItemService.updateQuantity(id, memberService.getCurrentMember().getId(), quantity);
        return CommonResult.success();
    }

    @Operation(summary = "获取购物车中指定商品的规格,用于重选规格")
    @GetMapping("/getProduct/{productId}")
    public CommonResult<CartProduct> getCartProduct(@PathVariable Long productId) {
        CartProduct cartProduct = cartItemService.getCartProduct(productId);
        return CommonResult.success(cartProduct);
    }

    @Operation(summary = "修改购物车中商品的规格")
    @PostMapping("/update/attr")
    public CommonResult<?> updateAttr(@RequestBody OmsCartItem cartItem) {
        cartItemService.updateAttr(cartItem);
        return CommonResult.success();
    }

    @Operation(summary = "删除购物车中的指定商品")
    @PostMapping("/delete")
    public CommonResult<?> delete(@RequestParam("ids") List<Long> ids) {
        cartItemService.delete(memberService.getCurrentMember().getId(), ids);
        return CommonResult.success();
    }

    @Operation(summary = "清空当前用户的购物车")
    @PostMapping("/clear")
    public CommonResult<?> clear() {
        cartItemService.clear(memberService.getCurrentMember().getId());
        return CommonResult.success();
    }

}
